import { Component } from "@angular/core";
import {NzModalService,NzModalSubject,NzNotificationService} from "ng-zorro-antd";
import { Router } from "@angular/router";
import {PointDefendBean} from "./bean/pointDefendBean";
import {HttpServcie} from "../../service/http.service";


@Component({
  selector: "point-defend",
  templateUrl: "point-defend.html",
  styleUrls: ["point-defend.scss"]
})
export class PointDefend {
  // 定义初始化实体类
  public pointDefendBean:PointDefendBean=new PointDefendBean();
  // 定义多选框选择事件
  public allChecked: any = false;
  // 定义多选框
  public indeterminate: any = false;
  // 定义后台返回的数据
  public pointData:any =[];
  // 定义再次封装数据为key、value
  public tempEditObject = {};
  // 定义界面 总条数
  public total:any=0;
  // 定义当前行
  public editRow = null;
  // 定义表头名称
  public modelTitle = "添加";
  // 定义模板类型
  public modal: any = 0;
  // 定义模板服务
  public currentModal: NzModalSubject;
  // 定义当前行的分数
  public rowMark:any;


  constructor(
    private httpServcie: HttpServcie,
    private _notification: NzNotificationService,
    private nsub: NzModalSubject,
    private router: Router,
    private modalService: NzModalService
  ) {
    JSON.parse(window.localStorage.getItem("user"));
    this.pointDefendBean.pageSize = 10;
    this.pointDefendBean.currentPage = 1;
  }

  ngOnInit() {

    /**
     * 初始化数据表格
     */
    this.queryData();


  }


  edit(data) {
    console.log("edit........");
    console.log(data);
    this.rowMark=data.mark;
    this.tempEditObject[ data.id ] = { ...data };
    this.editRow = data.id;
  }

  save(data) {
    Object.assign(data, this.tempEditObject[ data.id ]);
    this.editRow = null;
    console.log("save........");
    console.log(data);
    if(this.rowMark == data.mark){
      this._notification.create("success", "成功", "修改成功！");
    }else{
      this.updateData(data);
    }
  }

  cancel(data) {
    this.tempEditObject[ data.id ] = {};
    this.editRow = null;
  }

  //查询
  queryData() {

    this.httpServcie.pointDefendList(this.pointDefendBean).subscribe(data => {
      if (data.status == "200") {
        console.log(data);
        if (data.data) {
          this.pointData = data.data.list;
          this.total = data.data.total;

          this.pointData.forEach(item => {
            this.tempEditObject[ item.id ] = {};
          })

        } else {
          this.pointData = [];
        }
      } else {
        this._notification.create("error", "错误提示", data.message);
      }
    });

  }

  // 添加界面打开
  add(titleTpl, contentTpl, footerTpl) {
    this.modelTitle = "添加";
    this.modal = 0;
    this.currentModal = this.modalService.open({
      title: titleTpl,
      content: contentTpl,
      footer: footerTpl,
      maskClosable: false,
      width: 900,
      style: {
        top: "20px"
      },
      onOk() {
        console.log("Click ok");
      }
    });
  }

  // 添加后界面刷新
  addOutPut(event) {
    setTimeout(() => {
      /* destroy方法可以传入onOk或者onCancel。默认是onCancel */
      this.currentModal.destroy();
      this.currentModal = null;
    }, 200);
    // this.elevatorlBean.currentPage = 1;
    this.queryData();
  }

  // 删除
  delete(){
    const deleteList: any = [];
    const thiss:any = this;
    this.pointData.forEach(element => {
      if (element.checked) {
        deleteList.push(element.id + "");
      }
    });

    if (deleteList.length == 0) {
      // 异常
      this._notification.create("warning", "警告提示", "请选中一条数据！");
      return;
    }

    this.modalService.confirm({
      title: "您是否确认要删除这 " + deleteList.length + " 条内容",
      content: "<b>一旦删除，无法还原</b>",
      onOk() {
        let ob = thiss.httpServcie.deletePointDefend({ids: deleteList}); //批量删除
        ob.subscribe(
          data => {
            if (data.status == 200) {
              thiss._notification.create("success", "成功", "删除成功！");
              thiss.queryData();
            } else {
              thiss._notification.create("error", "错误提示", data.message);
            }
          },
          error => {
            thiss._notification.create("error", "错误提示", "删除失败！");
          }
        );
      },
      onCancel() {
      }
    });
  }

  // 修改
  updateData(data) {

    this.httpServcie.updatePointDefend(data).subscribe(data => {
      if (data.status == "200") {
        console.log(data);
        this._notification.create("success", "成功", "修改成功！");
        this.queryData();
      } else {
        this._notification.create("error", "错误提示", data.message);
      }
    });

  }







  //table加载数据时
  displayDataChange($event) {
    this.pointData = $event;
    this.refreshStatus();
  }
  //重置选择框
  refreshStatus() {
    const allChecked = this.pointData.every(
      value => value.disabled || value.checked
    );
    const allUnChecked = this.pointData.every(
      value => value.disabled || !value.checked
    );
    this.allChecked = allChecked;
    this.indeterminate = !allChecked && !allUnChecked;
  }
  //全选
  checkAll(value) {
    if (value) {
      this.pointData.forEach(data => {
        if (!data.disabled) {
          data.checked = true;
        }
      });
    } else {
      this.pointData.forEach(data => (data.checked = false));
    }
  }

  //分页
  nzPageSizeChange(event) {
    this.pointDefendBean.pageSize = event;
    this.pointDefendBean.currentPage = 1;
    this.queryData();
  }

  //输入框的方法
  nzPageIndexChange(event) {
    this.pointDefendBean.currentPage = event;
    this.queryData();
  }

  //当前页
  nzPageIndexClickChange(event) {
    this.pointDefendBean.currentPage = event;
    this.queryData();
  }

}
